from connector import DatabaseDriver


def get_cate_id_by_cate_name(driver: DatabaseDriver, cate_name: str):
    return driver.query(sql="select cate_id from category where cate_name='{}'".format(cate_name), log=True)


def get_cate_by_cate_name(driver: DatabaseDriver, cate_name: str):
    return driver.query(sql="select * from category where cate_name like '%{}%'".format(cate_name), log=True)


def get_cates(driver: DatabaseDriver) -> tuple:
    return driver.queryAll("select * from category", log=True)


def get_cate_by_id(driver: DatabaseDriver, cate_id: int) -> tuple:
    return driver.query("select * from category where cate_id={}".format(cate_id), log=True)


def update_cate(driver: DatabaseDriver, cate_id: int, cate_name: str) -> tuple:
    return driver.update("update category set cate_name='{}' where cate_id={}".format(cate_name, cate_id))


def del_cate(driver: DatabaseDriver, cate_id: int) -> int:
    return driver.delete("delete from category where cate_id={}".format(cate_id))


def add_cate(driver: DatabaseDriver, cate_name: str) -> int:
    if cate_name is not None:
        # 如果 cate_id 为 None，则省略插入语句中的 cate_id 字段
        return driver.insert("insert into category(cate_name) values('{}')".format(cate_name))
    else:
        return -1  # 返回 -1 表示插入失败
def get_all_types(driver: DatabaseDriver):
    try:
        return driver.queryAll("SELECT cate_id, cate_name FROM category")
    except Exception as e:
        print(f"Error fetching types: {e}")
        return []
